<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\sys\SysGroupModel;
use bc\util\backend\Page;

class GroupController extends BackendController
{
	
	/**
	 */
	public function actionIndex()
	{
		$model = new SysGroupModel ();
		$model->autoId = 'group_id';
		$params = ' 1 = 1';
		
		$page = $this->get ( 'page', 1 );
		// $model->sort = 'prize_id DESC';
		$list = $model->getList ( $params, $page, self::PAGE_SIZE );
		
		$total = $model->getTotal ( $params );
		
		$pagination = new Page ( $total, self::PAGE_SIZE );
		
		$view ['list'] = $list;
		$view['pagination'] = $pagination;
		
		return $this->render ( 'index', $view );
	}
	
	/**
	 * 
	 */
	public function actionAdd()
	{
		return $this->render ( 'add' );
	}
	
	/**
	 * 
	 */
	public function actionEdit()
	{
		$id = $this->get ( 'id' );
		if (! $id)
			$this->redirect ( '/' );
		
		$model = new SysGroupModel ();
		$model->autoId = 'group_id';
		$row = $model->getRowById ( $id );
		
		if (! $row)
			$this->redirect ( '/' );
		
		$view ['row'] = $row;
		
		return $this->render ( 'edit', $view );
	}
	
	/**
	 * 
	 */
	public function actionSave()
	{
		if ($this->getRequest ()->getMethod () === 'POST')
		{
			$model = new SysGroupModel ();
			$id = $this->post ( 'id', 0 );
			$model->autoId = 'group_id';
			
			$data ['group_name'] = $this->post ( 'group_name', '' );
			$data ['group_status'] = $this->post ( 'group_status', 1 );
			
			
			$row = $model->getRow(['group_name' => $data['group_name']]);
			if($row)
			{
				$output ['msg'] = '权限组名称重复!';
			}
			else 
			{
				if ($id)
				{
					$model->modify ( $data, $id );
				}
				else
				{
					$model->add ( $data );
				}
				$output ['msg'] = '操作成功!';
			}
			
			
		} 
		else
		{
			$output ['msg'] = '非法请求!';
		}
		
		$output ['url'] = '/group';
		$this->forward ( $output );
	}
}

